<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #res {
            width: 200px;
            height: 100px;
            border: 1px solid red;
        }
    </style>
</head>

<body>
    <div id="res"></div>
    <script>
        //绑定键盘按下事件
        var res = document.getElementById('res');
        window.onkeydown = function() {
            // 1.发送请求
            const xhr = new XMLHttpRequest();
            //设置响应体的数据类型
            xhr.responseType = 'json';
            // 2.初始化
            xhr.open('GET', 'http://127.0.0.1:8080/json-server');
            //3.发送
            xhr.send();
            //4.事件绑定
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status >= 200 && xhr.status < 300) {
                        // res.innerHTML = xhr.response;
                        // 手动对数据进行转化
                        // let data = JSON.parse(xhr.response);
                        //自动转化
                        res.innerHTML = xhr.response.name;
                    }
                }
            }
        }
    </script>
</body>

</html>